<template>
  <div class="detail-container">
    <div class="approval">
      <el-button type="primary" @click="showApproval" v-if="fromtype !== 'zf' && fromtype !== 'fsh'" :disabled="isZF">
        {{ translate('审核') }}
      </el-button>
      <el-button type="primary" @click="showFsh" v-if="fromtype === 'fsh'" :disabled="isZF">
        {{ translate('反审核') }}
      </el-button>
      <el-button type="primary" @click="showDY" v-if="formcode === 'Wms_PickingBillMaster'">
        {{ translate('打印') }}
      </el-button>
      <el-button type="primary" @click="showZf" v-if="fromtype === 'zf'" :disabled="isZF">
        {{ translate('作废') }}
      </el-button>
    </div>
    <i v-if="status === 1" class="iconfont icon-shenhezhong iconcommon"></i>
    <i v-if="status === 2" class="iconfont icon-yishenhe iconcommon2"></i>
    <i  v-if="status === 3" class="iconfont icon-weishenhe iconcommon3"></i>
    <i  v-if="status === 4" class="iconfont icon-yizuofei iconcommon3"></i>
    <InboundBill v-if="formcode === 'Wms_IncomeBillMaster'" :id="formId" />
    <OutboundBill v-if="formcode === 'Wms_OutboundBillMaster'" :id="formId" />
    <CheckOrder v-if="formcode === 'Wms_CheckOrderMaster'" :id="formId" />
    <TransferOrder v-if="formcode === 'Wms_TransferOrderMaster'" :id="formId" />
    <PlanBillMaster v-if="formcode === 'Wms_ItemPlanBillMaster'" :id="formId" />
    <PickingBillMaster
      v-if="formcode === 'Wms_PickingBillMaster'"
      :id="formId"
    />
    <DiffIncomeBill
      v-if="formcode === 'Wms_DiffIncomeBillMaster'"
      :id="formId"
    />
    <DiffoutboundBill
      v-if="formcode === 'Wms_DiffIncomeBillMaster'"
      :id="formId"
    />
    <ScrapBillMaster v-if="formcode === 'Wms_ScrapBillMaster'" :id="formId" />
    <WmsDemandPlanBillMaster
      v-if="formcode === 'Wms_DemandPlanBillMaster'"
      :id="formId"
    />
    <WmsItemCodeApplyMaster
      v-if="formcode === 'Wms_ItemCodeApplyMaster'"
      :id="formId"
    />
    <JNZJOperatorCardMaster
      v-if="formcode === 'JNZJ_OperatorCardMaster'"
      :id="formId"
    />
    <!-- <ArrivalConfirm :id="formId" /> -->
    <Workflow
      :drawershow="isdrawershow"
      :formcode="formcode"
      :formid="formId"
      @handclose="handclose"
      @submitfn="submitFn"
    />
     <!-- 打印页面 -->
     <el-dialog v-model="isPrint" center width="1450px" @close="isPrint = false" :close-on-click-modal="false" >
      <print
        ref="tableRef"
        :chileddetail="chiledPrintdetail"
        :form="form"
        fromprint="goodsList"
        :supplierdata="SupplierData"
      />
      <template #footer>
        <el-button @click="isPrint = false">{{ translate('取消') }}</el-button>
        <el-button type="primary" @click="submitPrint">
          {{ translate('打印') }}
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import InboundBill from './components/inboundBill.vue'
  import OutboundBill from './components/outboundBill.vue'
  import PlanBillMaster from './components/planBillMaster.vue'
  import PickingBillMaster from './components/pickingBillMaster.vue'
  import CheckOrder from './components/checkOrder.vue'
  import TransferOrder from './components/transferOrder.vue'
  import DiffIncomeBill from './components/diffIncomeBill.vue'
  import DiffoutboundBill from './components/diffoutboundBill.vue'
  import ArrivalConfirm from './components/arrivalConfirm.vue'
  import Workflow from '@/views/components/wordflow/index.vue'
  import ScrapBillMaster from './components/scrapBillMaster.vue'
  import WmsDemandPlanBillMaster from './components/WmsDemandPlanBillMaster.vue'
  import WmsItemCodeApplyMaster from './components/WmsItemCodeApplyMaster.vue'
  import JNZJOperatorCardMaster from './components/JNZJOperatorCardMaster.vue'
  import print from './components/print'
  import VabPrint from '@/plugins/VabPrint'
  import { translate } from '@/i18n'
  import { getinstance, OutboundExamine, OutboundCancel, OutboundVoid, ZFPickingVoid, AuditDemandPlan, AuditItemCodeApply, pickingbillUpdateDetail, AduitOPCardData } from '@/api/form'
  const route = useRoute()
  const formcode: any = ref('')
  const formId: any = ref('')
  const isshow = ref(false)
  const status = ref('')
  const fromtype = ref('')
  const isZF = ref(false)
  const isPrint = ref(false)
  const chiledPrintdetail = ref([])
  const tableRef = ref(null)
  const form = ref({})
  if (route.query) {
    formId.value = route.query.formId
    formcode.value = route.query.formcode
    fromtype.value = route.query.type
    if(route.query.statusSH === '已审核') {
      status.value = 2
    }else if(route.query.statusSH === '已作废' || route.query.statusSH === '作废'  ){
      isZF.value = true
      status.value = 4
    }else if(route.query.statusSH === '已反审核'){
      status.value = 3
    }else {
      status.value = ''
    }
  }
  const showDY = async () => {
    const params = {
      pageNo: 1,
      pageSize: 100,
      total: 0,
      tableName: '',
      sort: '',
      order: '',
      wheres: '',
      export: true,
      value: formId.value,
    }
    const data = await pickingbillUpdateDetail(params)
    if(data.rows && data.rows.length>0) {
      data.rows.map((item,index) =>{
        item.childList = []
        if(data.extra && data.extra.length>0){
          data.extra.map(item1 => {
            if(item.ItemCode === item1.ItemCode) {
              item.childList.push(item1)
            }
          })
        }
      })
    }
    isPrint.value = true
    chiledPrintdetail.value = data.rows || []
    form.value = data.master || {}
  }
  const  submitPrint = async () => {
        document.title = '领料单'
        await VabPrint(tableRef.value)
        isPrint.value = false
  }
  const getinstances = async () => {
    const params = {
      code: formcode.value,
      formId: formId.value,
    }
    const data = await getinstance(params)
    if (data.code === '200') {
      status.value = data.data?.data?.status
      if (data.data?.data?.status === 2) {
        isshow.value = true
        console.log('已审核')
      }
      // console.log(formcode.value)
      // console.log(formcode.value === 'Wms_ItemCodeApplyMaster')
      // if(formcode.value === 'Wms_ItemCodeApplyMaster') {
      //       AuditItemCodeApplys()
      // }
    }
  }
  const getinstances1 = async () => {
    const params = {
      code: formcode.value,
      formId: formId.value,
    }
    const data = await getinstance(params)
    if (data.code === '200') {
      status.value = data.data?.data?.status
      if (data.data?.data?.status === 2) {
        isshow.value = true
        console.log('已审核')
        if(formcode.value === 'JNZJ_OperatorCardMaster') {
          AduitOPCardDatas()
      }
      }
      console.log(formcode.value)
      console.log(formcode.value === 'Wms_ItemCodeApplyMaster')
      if(formcode.value === 'Wms_ItemCodeApplyMaster') {
            AuditItemCodeApplys()
      }
    }
    isdrawershow.value = false
  }

  const isdrawershow = ref(false)
  const showApproval = () => {
    console.log(formcode.value)
    if(formcode.value === 'Wms_OutboundBillMaster') {
      wsmboundbillMaster()
    }else {
      isdrawershow.value = true
    }
  }
  const $baseConfirm = inject('$baseConfirm') as any
  const wsmboundbillMaster = () => {
    $baseConfirm(translate('你确定提交吗'), null, async () => {
      console.log(formId.value)
      isZF.value =true
      OutboundExamines()
    })
  }
  const AuditDemandPlanFn = () => {
    $baseConfirm(translate('你确定提交吗'), null, async () => {
      console.log(formId.value)
      isZF.value =true
      AuditDemandPlans()
    })
  }
  const handclose = () => {
    isdrawershow.value = false
  }
  const submitFn = () => {
    getinstances1()
  }
  const AuditDemandPlans = async () => {
        const params = {
          formId: formId.value,
        }
        const data = await AuditDemandPlan(params)
        if (data.result.code === '200') {
          status.value = 2
          ElMessage({
            message:'提交成功',
            type:'success',
          })
        }else {
          ElMessage({
            message:data.result.message,
            type:'error',
          })
        }
        setTimeout( () => {
          isZF.value =false
        },30000)
  }
  const AuditItemCodeApplyFn = () => {
    $baseConfirm(translate('你确定提交吗'), null, async () => {
      console.log(formId.value)
      isZF.value =true
      AuditItemCodeApplys()
    })
  }
  const AuditItemCodeApplys = async () => {
        const params = {
          formId: formId.value,
        }
        const data = await AuditItemCodeApply(params)
        if (data.result.status) {
          status.value = 2
          ElMessage({
            message:'提交成功',
            type:'success',
          })
        }else {
          ElMessage({
            message:data.result.message,
            type:'error',
          })
        }
        setTimeout( () => {
          isZF.value =false
        },30000)
  }
  const AduitOPCardDatas = async () => {
        const params = {
          formId: formId.value,
        }
        const data = await AduitOPCardData(params)
        if (data.result.status) {
          status.value = 2
          ElMessage({
            message:'提交成功',
            type:'success',
          })
        }else {
          ElMessage({
            message:data.result.message,
            type:'error',
          })
        }
        setTimeout( () => {
          isZF.value =false
        },30000)
  }
  const showFsh = () => {
    $baseConfirm(translate('你确定提交吗'), null, async () => {
      fshDis()
    })
  }
  const showZf = () => {
    $baseConfirm(translate('你确定作废吗'), null, async () => {
      if(formcode.value === 'Wms_PickingBillMaster') {
        zfBill()
      }else {
        zfDis()
      }
    })
  }
  const zfBill = async () => {
    const params = {
      formId: formId.value
    }
    const data = await ZFPickingVoid(params)
          if(data.result.code === '200') {
            ElMessage({
              message: '操作成功' ,
              type: 'seccess'
            })
          }else {
            ElMessage({
              message: data.result.message ,
            })
         }
  }
  const zfDis = async () => {
    const params = {
            formId: formId.value
          }
          const data = await OutboundVoid(params)
          if(data.result.code === '200') {
            ElMessage({
              message: '操作成功' ,
              type: 'seccess'
            })
          }else {
            ElMessage({
              message: data.result.message ,
            })
          }
  }
  const fshDis = async () => {
    const params = {
          formId: formId.value,
        }
        const data = await OutboundCancel(params)
        ElMessage({
          message: data.result.message ? data.result.message : '未知错误',
        })
  }
  const OutboundExamines = async () => {
        const params = {
          code: 'Wms_OutboundBillMaster',
          formId: formId.value,
        }
        const data = await OutboundExamine(params)
        if (data.result.code === '200') {
          status.value = 2
          ElMessage({
            message:'提交成功',
            type:'success',
          })
        }else {
          ElMessage({
            message:data.result.message,
            type:'error',
          })
        }
        setTimeout( () => {
          isZF.value =false
        },30000)
  }
  onMounted(() => {
    if(route.query.formcode !== 'Wms_OutboundBillMaster' && route.query.formcode !== 'Wms_DemandPlanBillMaster') {
      getinstances()
    }
  })
</script>

<style lang="scss" scoped>
  .detail-container {
    .approval {
      display: flex;
      padding: 10px;
      // flex-direction: row-reverse;
      // padding-right: 10%;
      // padding-bottom: 20px;
    }

    .iconcommon {
      position: absolute;
      top: 28px;
      right: 70px;
      font-size: 60px;
      color: blue;
    }
    .iconcommon3 {
      position: absolute;
      top: 28px;
      right: 70px;
      font-size: 60px;
    }
    .iconcommon2 {
      position: absolute;
      top: 28px;
      right: 70px;
      font-size: 60px;
      color: red;
    }
  }
</style>
